$(function () {
    $("#role_datagrid").datagrid({
        fit:true,
        url:'/role/list',
        rownumbers:true,
        singleSelect:true,
        pagination:true,
        checkOnSelect:true,
        fitColumns:true,
        toolbar:role_datagrid_Btn,
        columns:[
            [
                {field:'sn',title:'角色编号',align:'center',width:10},
                {field:'name',title:'角色名称',align:'center',width:10},
            ]
        ]
    })
    $("#role_dialog").dialog({
        title:'添加角色',
        width:600,
        height:600 ,
        buttons:'#role_dialog_bt',
        closed: true
    })
    $("#searchBtn").textbox({
        onClickButton:function () {
           var keyword =  $("#searchBtn").val();
            $("#role_datagrid").datagrid("load",{
                keyword:keyword
            })
        }
    })
    $("#allPermissions").datagrid({
        fitColumns: true,
        singleSelect: true,
        rownumbers: true,
        striped:true,
        width: 250,
        height: 430,
        url:'/permission/queryAllPermission',
        title:'所有权限',
        columns:[
            [
                {field: 'name', width: 1},
            ]
        ],

        onDblClickRow:function(index,row){
            $("#selfPermissions").datagrid("appendRow",row);
            $("#allPermissions").datagrid("deleteRow",index);
        },
        onLoadSuccess: function (data) {
            allData = $.extend(true,{},data);
        }
    })
    $("#selfPermissions").datagrid({
        fitColumns:true,
        striped:true,
        selectOnCheck:true,
        singleSelect: true,
        rownumbers:true,
        width: 250,
        height: 430,
        title:'自身权限',
        columns:[
            [
                {field: 'name', width: 1},
            ]
        ],
        onDblClickRow: function (index,row) {
            $("#allPermissions").datagrid("appendRow",row);
            $("#selfPermissions").datagrid("deleteRow",index);
        },
        onLoadSuccess: function (data) {
            var ids = $.map(data.rows, function (item) {
                return item.id
            });
            //使用$.inArray进行筛选已经有的值
            var allPermissionRows =$("#allPermissions").datagrid("getRows");
            for (var i =allPermissionRows.length-1;i>=0;i--){
                if($.inArray(allPermissionRows[i].id,ids)>=0){
                    $("#allPermissions").datagrid("deleteRow",i);
                }
            }
        }
    })

})

function stateFormatter(value){
    if(value ==1){
        return "<font color='#adff2f'>是</font>"
    }else{
        return "<font color='red'>否</font>"
    }
}

function role_add(){
        $("#role_dialog").dialog("open")
        $("#role_form").form("clear")
        $("#selfPermissions").datagrid("loadData",{total:0,rows:[]})
}



function deptFormatter(value) {
    return value.name;
}
//数据编辑===================================================================
function role_edit() {
    //在这里getSelect就是获取到选择到的所有数据信息
    var rowData = $("#role_datagrid").datagrid("getSelected");
    if(rowData){
        $("#role_dialog").dialog("open");
       //进行返回对象,并且通过对象进行返回,通过属性
        $("#allPermissions").datagrid("loadData",allData);
       var options =  $("#selfPermissions").datagrid("options");
        options.url="/permission/queryPageResultByRoleId?roleId="+rowData.id;
        $("#selfPermissions").datagrid("load");
        //数据回显
        if(rowData.dept){
            rowData["dept.id"] = rowData.dept.id;
        }
        $("#role_form").form("load",rowData);
    }else{
        $.messager.alert("温馨提示","请选择需要编辑的数据","warning");
    }
}
function role_cancel() {
    //进行取消窗口页面
    $("#role_dialog").dialog("close")
}
function role_reload() {
    //进行刷新表单
    $("#role_datagrid").datagrid("reload");
}

//添加==================================================================
function role_save() {
     var url;
     var idVal = $("[name=id]").val();
    if(idVal){
        //进行判断
        url = "/role/update"
    }else{
        url ="/role/save"
    }
    $("#role_form").form("submit",{
     url:url,
        /*进行额外数据进行提交,并且通过*/
        onSubmit:function (param) {
           var rows =  $("#selfPermissions").datagrid("getRows");
            //遍历,封装数据
            for(var i =0 ;i<rows.length; i++){
                param["permissions["+i+"].id"] = rows[i].id;
            }
        },
        success:function (data) {
           data=  $.parseJSON(data);
           if(data.success){
               $.messager.alert("温馨提示",data.msg,"info",function () {
                   //刷新数据表格
                  $("#role_datagrid").datagrid("reload");
                   //关闭对话框
                   $("#role_dialog").dialog("close")
               });
               }
               else {
                    $.messager.alert("温馨提示",data.msg,"error")
           }
        }
    });
}
//删除==================================================================
function role_remove(){
    var rowData = $("#role_datagrid").datagrid("getSelected");
    if(rowData){
        $.messager.confirm('温馨提示','您确认想要删除记录吗？',function(yes){
            if(yes){
                $.post("/role/delete?id="+rowData.id,function (data) {
                    if(data.success){
                        $.messager.alert("温馨提示",data.msg,"info",function () {
                            $("#role_datagrid").datagrid("reload");
                        });
                    }else {
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                },"json");
            }
        });
    }else{
        $.messager.alert("温馨提示","请选择需要删除的数据","warning");
    }
}